package cc.rengu.igas.bomp.common.dao.impl;

import cc.rengu.igas.bomp.common.dao.ProdInfoMapper;
import cc.rengu.igas.bomp.common.entity.ProdInfo;
import cc.rengu.oltp.service.common.constant.AppConfigConstant;
import cc.rengu.oltp.service.common.constant.AppParamConstant;
import cc.rengu.oltp.utility.bean.PageInfo;
import cc.rengu.oltp.utility.bean.PageResult;
import cc.rengu.oltp.utility.util.DbsUtil;
import cc.rengu.oltp.utility.util.XmlConfigUtil;
import cc.rengu.oltp.utility.util.XmlTreeUtil;
import cc.rengu.utility.cache.UnifiedCache;
import cc.rengu.utility.log.RgLog;
import cc.rengu.utility.log.RgLogger;

import java.util.List;
import java.util.stream.Collectors;

/**
 * Created by 王成 on 2020/3/6
 */
public class ProdInfoMapperImpl implements ProdInfoMapper {
    private RgLogger rglog = RgLog.getLogger(this.getClass().getName());
    private String dbPoolName = XmlConfigUtil.getXmlConfigCacheKeyValue(AppConfigConstant.DATABASE_CONF_NODE, AppConfigConstant.MCMP_POOL_NAME);

    @Override
    public int insertProdInfo(ProdInfo prodInfo) throws Exception {
        XmlTreeUtil xmlTreeUtil = new XmlTreeUtil();
        String corporation = xmlTreeUtil.getXmlTreeStringValue(AppParamConstant.CORPORATION);
        if (null != corporation && !corporation.isEmpty()) {
            String dbPoolCashNode = corporation.trim() + AppParamConstant.DATABASE_CONF_NODE;
            dbPoolName = XmlConfigUtil.getXmlConfigCacheKeyValue(dbPoolCashNode, AppConfigConstant.MCMP_POOL_NAME);
        }
        DbsUtil dbsUtil = new DbsUtil(dbPoolName);
        String sqlStr = (String) UnifiedCache.get(AppParamConstant.SQL_MAPPER_CACHE, "insertProdInfo");
        int iReturnCode = dbsUtil.dbsExecuteSql(sqlStr, prodInfo, null);
        if (0 != iReturnCode) {
            rglog.error("登记产品信息失败,instId:<{}>,prodCode:<{}>,baseProdCode:<{}>,iReturnCode:<{}>", prodInfo.getInstId(), prodInfo.getProdCode(), prodInfo.getBaseProdCode(), iReturnCode);
        }
        return iReturnCode;
    }

    @Override
    public int updateProdInfoById(ProdInfo prodInfo) throws Exception {
        XmlTreeUtil xmlTreeUtil = new XmlTreeUtil();
        String corporation = xmlTreeUtil.getXmlTreeStringValue(AppParamConstant.CORPORATION);
        if (null != corporation && !corporation.isEmpty()) {
            String dbPoolCashNode = corporation.trim() + AppParamConstant.DATABASE_CONF_NODE;
            dbPoolName = XmlConfigUtil.getXmlConfigCacheKeyValue(dbPoolCashNode, AppConfigConstant.MCMP_POOL_NAME);
        }
        DbsUtil dbsUtil = new DbsUtil(dbPoolName);
        String sqlStr = (String) UnifiedCache.get(AppParamConstant.SQL_MAPPER_CACHE, "updateProdInfoById");
        int iReturnCode = dbsUtil.dbsExecuteSql(sqlStr, prodInfo, null);
        if (0 != iReturnCode) {
            rglog.error("登记产品信息失败,instId:<{}>,prodCode:<{}>,baseProdCode:<{}>,iReturnCode:<{}>", prodInfo.getInstId(), prodInfo.getProdCode(), prodInfo.getBaseProdCode(), iReturnCode);
        }
        return iReturnCode;
    }

    @Override
    public ProdInfo selectProdInfoById(String instId, String id) throws Exception {
        XmlTreeUtil xmlTreeUtil = new XmlTreeUtil();
        String corporation = xmlTreeUtil.getXmlTreeStringValue(AppParamConstant.CORPORATION);
        if (null != corporation && !corporation.isEmpty()) {
            String dbPoolCashNode = corporation.trim() + AppParamConstant.DATABASE_CONF_NODE;
            dbPoolName = XmlConfigUtil.getXmlConfigCacheKeyValue(dbPoolCashNode, AppConfigConstant.MCMP_POOL_NAME);
        }
        DbsUtil dbsUtil = new DbsUtil(dbPoolName);
        String sqlStr = (String) UnifiedCache.get(AppParamConstant.SQL_MAPPER_CACHE, "selectProdInfoById");
        ProdInfo prodInfo = new ProdInfo();
        prodInfo.setInstId(instId);
        prodInfo.setId(id);
        int iReturnCode = dbsUtil.dbsExecuteSql(sqlStr, prodInfo, prodInfo);
        if (0 != iReturnCode) {
            rglog.error("获取产品信息失败,instId:<{}>,id:<{}>,iReturnCode:<{}>", instId, id, iReturnCode);
            return null;
        }
        return prodInfo;
    }

    @Override
    public ProdInfo selectProdInfoByPrimaryKey(String instId, String prodCode, String baseProdCode) throws Exception {
        XmlTreeUtil xmlTreeUtil = new XmlTreeUtil();
        String corporation = xmlTreeUtil.getXmlTreeStringValue(AppParamConstant.CORPORATION);
        if (null != corporation && !corporation.isEmpty()) {
            String dbPoolCashNode = corporation.trim() + AppParamConstant.DATABASE_CONF_NODE;
            dbPoolName = XmlConfigUtil.getXmlConfigCacheKeyValue(dbPoolCashNode, AppConfigConstant.MCMP_POOL_NAME);
        }
        DbsUtil dbsUtil = new DbsUtil(dbPoolName);
        String sqlStr = (String) UnifiedCache.get(AppParamConstant.SQL_MAPPER_CACHE, "selectProdInfoByPrimaryKey");
        ProdInfo prodInfo = new ProdInfo();
        prodInfo.setInstId(instId);
        prodInfo.setProdCode(prodCode);
        prodInfo.setBaseProdCode(baseProdCode);
        int iReturnCode = dbsUtil.dbsExecuteSql(sqlStr, prodInfo, prodInfo);
        if (0 != iReturnCode) {
            rglog.error("获取产品信息失败,instId:<{}>,prodCode:<{}>,baseProdCode:<{}>,iReturnCode:<{}>", instId, prodCode, baseProdCode, iReturnCode);
            return null;
        }
        return prodInfo;
    }

    @Override
    public List<ProdInfo> selectProdInfoByStandardProdCode(String instId, String prodCode) throws Exception {
        XmlTreeUtil xmlTreeUtil = new XmlTreeUtil();
        String corporation = xmlTreeUtil.getXmlTreeStringValue(AppParamConstant.CORPORATION);
        if (null != corporation && !corporation.isEmpty()) {
            String dbPoolCashNode = corporation.trim() + AppParamConstant.DATABASE_CONF_NODE;
            dbPoolName = XmlConfigUtil.getXmlConfigCacheKeyValue(dbPoolCashNode, AppConfigConstant.MCMP_POOL_NAME);
        }
        DbsUtil dbsUtil = new DbsUtil(dbPoolName);
        String sqlStr = (String) UnifiedCache.get(AppParamConstant.SQL_MAPPER_CACHE, "selectProdInfoByStandardProdCode");
        ProdInfo prodInfo = new ProdInfo();
        prodInfo.setInstId(instId);
        prodInfo.setProdCode(prodCode);
        List<Object> objectList = dbsUtil.dbsFetchLimitListData(sqlStr, prodInfo, ProdInfo.class);
        if (null == objectList || objectList.isEmpty()) {
            rglog.error("获取产品信息失败,instId:<{}>,prodCode:<{}>", instId, prodCode);
            return null;
        }
        return objectList.stream().map(item -> (ProdInfo) item).collect(Collectors.toList());
    }

    @Override
    public List<ProdInfo> selectProdInfoByInstId(String instId) throws Exception {
        XmlTreeUtil xmlTreeUtil = new XmlTreeUtil();
        String corporation = xmlTreeUtil.getXmlTreeStringValue(AppParamConstant.CORPORATION);
        if (null != corporation && !corporation.isEmpty()) {
            String dbPoolCashNode = corporation.trim() + AppParamConstant.DATABASE_CONF_NODE;
            dbPoolName = XmlConfigUtil.getXmlConfigCacheKeyValue(dbPoolCashNode, AppConfigConstant.MCMP_POOL_NAME);
        }
        DbsUtil dbsUtil = new DbsUtil(dbPoolName);
        String sqlStr = (String) UnifiedCache.get(AppParamConstant.SQL_MAPPER_CACHE, "selectProdInfoByInstId");
        ProdInfo prodInfo = new ProdInfo();
        prodInfo.setInstId(instId);
        List<Object> objectList = dbsUtil.dbsFetchLimitListData(sqlStr, prodInfo, ProdInfo.class);
        if (null == objectList || objectList.isEmpty()) {
            rglog.error("获取产品信息失败,instId:<{}>", instId);
            return null;
        }
        return objectList.stream().map(item -> (ProdInfo) item).collect(Collectors.toList());
    }

    @Override
    public PageResult selectProdInfoByInstId(String instId, PageInfo pageInfo) throws Exception {
        XmlTreeUtil xmlTreeUtil = new XmlTreeUtil();
        String corporation = xmlTreeUtil.getXmlTreeStringValue(AppParamConstant.CORPORATION);
        if (null != corporation && !corporation.isEmpty()) {
            String dbPoolCashNode = corporation.trim() + AppParamConstant.DATABASE_CONF_NODE;
            dbPoolName = XmlConfigUtil.getXmlConfigCacheKeyValue(dbPoolCashNode, AppConfigConstant.MCMP_POOL_NAME);
        }
        DbsUtil dbsUtil = new DbsUtil(dbPoolName);
        String sqlStr = (String) UnifiedCache.get(AppParamConstant.SQL_MAPPER_CACHE, "selectProdInfoByInstId");
        ProdInfo prodInfo = new ProdInfo();
        prodInfo.setInstId(instId);
        return dbsUtil.dbsFetchPageListData(sqlStr, prodInfo, ProdInfo.class, pageInfo);
    }


    @Override
    public List<String> selectWechatChannelIdByInstId(String instId, String prodCode) throws Exception {
        XmlTreeUtil xmlTreeUtil = new XmlTreeUtil();
        String corporation = xmlTreeUtil.getXmlTreeStringValue(AppParamConstant.CORPORATION);
        if (null != corporation && !corporation.isEmpty()) {
            String dbPoolCashNode = corporation.trim() + AppParamConstant.DATABASE_CONF_NODE;
            dbPoolName = XmlConfigUtil.getXmlConfigCacheKeyValue(dbPoolCashNode, AppConfigConstant.MCMP_POOL_NAME);
        }
        DbsUtil dbsUtil = new DbsUtil(dbPoolName);
        String sqlStr = (String) UnifiedCache.get(AppParamConstant.SQL_MAPPER_CACHE, "selectWechatChannelIdByInstId");
        ProdInfo prodInfo = new ProdInfo();
        prodInfo.setInstId(instId);
        List<Object> objectList = dbsUtil.dbsFetchLimitListData(sqlStr, prodInfo, String.class);
        if (null == objectList || objectList.isEmpty()) {
            rglog.error("获取产品信息失败,instId:<{}>", instId);
            return null;
        }
        return objectList.stream().map(item -> (String) item).collect(Collectors.toList());
    }
}
